package com.carnation.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.carnation.bean.LocalFileInfo;
import com.carnation.service.FileService;
import com.carnation.service.impl.FileServiceImpl;
import com.google.gson.Gson;

public class LocalFileServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private Logger logger= Logger.getLogger(LocalFileServlet.class);
	
	@Override
	protected void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String path = httpServletRequest.getParameter("path");
		if(path == null || path.isEmpty()){
			path = "/";
		}
		logger.info(path);
		FileService fileService = new FileServiceImpl();
		String realPath = fileService.getRealPath(path);
		logger.info("local file realpath = " + realPath);
		if(realPath == null){
			// TODO 错误情况
			return ;
		}
		else{
			List<LocalFileInfo> list = fileService.getLocalFileInfoByRealPath(realPath);
			Gson gson = new Gson();
			httpServletResponse.setCharacterEncoding("UTF-8");
			try {
				httpServletResponse.getWriter().write(gson.toJson(list));
				httpServletResponse.getWriter().close();
			} catch (IOException e) {
				logger.error("写返回报文出错",e);
			}
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		doPost(httpServletRequest, httpServletResponse);
	}
}
